package org.jash.shop2112.entity

import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import kotlin.math.floor

class Cart:BaseObservable() {
    val data:MutableList<CartItem> = mutableListOf()
    @get:Bindable
    var all:Int
        get() = when(data.filter { it.checked }.size) {
            0 -> 0
            data.size -> 1
            else -> 2
        }
        set(value) {
            if (value != 2) {
                data.forEach {
                    it.checked = value == 1
                }
            }
        }
    @get:Bindable("all")
    val total
        get() = data.filter { it.checked }.map { it.subTotal }.sum()
    @get:Bindable("all")
    val coupon
        get() = data.filter { it.checked }.map { it.subTotal }.sum().let { it - floor(it.toDouble() / 5000).toFloat() * 200 }
}